<template>
  <div class="container" ref="container">
    <div class="wrapper">
      <div class="eva_content">
        <div class="content_hd">
          <div class="btn_choses">
            <span
              class="all"
              :class="{ btn_color: selectCommentsType === 2 }"
              @click="setSelectCommentsType(2)"
              >全部 {{ shopRatings.length }}</span
            >
            <span
              class="satisfied "
              :class="{ btn_color: selectCommentsType === 0 }"
              @click="setSelectCommentsType(0)"
              >满意 {{ satisfied }}
            </span>
            <span
              class="no_satisfied"
              :class="{ btn_color: selectCommentsType === 1 }"
              @click="setSelectCommentsType(1)"
              >不满意 {{ shopRatings.length - satisfied }}
            </span>
          </div>
          <div class="just_look">
            <i
              ><van-icon
                name="checked"
                :class="{ icon_color: isChangeColor }"
                @click="switchType"
            /></i>
            <span>只看有内容的评价</span>
          </div>
        </div>
      </div>
      <div class="comment_bd">
        <ul class="comments">
          <li
            class="items"
            v-for="(ratings, index) in filterRatings"
            :key="index"
          >
            <div class="item-l">
              <i class="header_pic"><img :src="ratings.avatar" alt=""/></i>
            </div>
            <div class="items_r">
              <span
                ><h3>{{ ratings.username }}</h3>
                <strong class="time">{{
                  ratings.rateTime | format('yyyy-MM-dd hh:mm:ss')
                }}</strong></span
              >
              <div class="stars">
                <van-rate
                  v-model="comment_stars"
                  :size="10"
                  color="#ffd21e"
                  void-icon="star"
                  void-color="#eee"
                  class="comment_stars"
                />
                <span class="score">{{ ratings.score }}分</span>
              </div>
              <p class="instructions">
                {{ ratings.text }}
              </p>
              <span class="chose_label">
                <i class="praise"
                  ><van-icon name="thumb-circle" color="gold"
                /></i>
                <van-tag
                  class="labels"
                  v-for="(i, id) in ratings.recommend"
                  :key="id"
                  >{{ i }}</van-tag
                >
              </span>
            </div>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState, mapGetters } from 'vuex'
import BScroll from 'better-scroll'
export default {
  data() {
    return {
      comment_stars: 5,
      scroll: '',
      // 是否显示评价文本
      isChangeColor: false,
      // 选择评论类型  0-满意 1-不满意 2全部
      selectCommentsType: 2
    }
  },
  methods: {
    // 切换评价类型
    switchType() {
      this.isChangeColor = !this.isChangeColor
    },
    // 选择评论的类型
    setSelectCommentsType(selectType) {
      this.selectCommentsType = selectType
    }
  },
  computed: {
    ...mapState(['shopRatings']),
    ...mapGetters(['satisfied']),
    // 过滤出对应的评论类型
    filterRatings() {
      const { selectCommentsType, isChangeColor, shopRatings } = this
      return shopRatings.filter(ratings => {
        const { rateType, text } = ratings
        if (
          (selectCommentsType === 2 || selectCommentsType === rateType) &&
          (!isChangeColor || text.length > 0)
        ) {
          return ratings
        }
      })
    }
  },
  mounted() {
    this.$nextTick(() => {
      const container = this.$refs.container
      this.scroll = new BScroll(container)
    })
  }
}
</script>

<style lang="less" scoped>
.container {
  height: 270px;
  overflow: hidden;
  background-color: #fff;
}
.eva_content {
  margin-top: 25px;
  box-shadow: 0px -1px 2px rgba(0, 0, 0, 0.1);
  background-color: #fff;
  padding-left: 13px;
  .content_hd {
    height: 150px;
    box-shadow: 0 2px 1px rgba(0, 0, 0, 0.1);
    .no_satisfied,
    .satisfied,
    .all {
      display: inline-block;
      height: 30px;
      width: 70px;
      background-color: #f5f5f5;
      text-align: center;
      line-height: 30px;
      margin-top: 20px;
      font-size: 14px;
    }
    .satisfied {
      margin: 0 13px;
    }
    .btn_color {
      background-color: green !important;
      color: #fff !important;
    }
  }
  .just_look {
    margin-top: 70px;
    color: #999;
    font-size: 13px;
    span {
      display: inline-block;
      padding-left: 5px;
      vertical-align: middle;
    }
    .van-icon {
      vertical-align: middle;
      display: inline-block;
      padding-left: 10px;
      font-size: 17px;
    }
    .icon_color {
      color: green;
    }
  }
}
.comment_bd {
  .comments {
    .items {
      border-top: 1px solid #ccc;
      background-color: #fff;
      height: 100%;
      overflow: hidden;
      .item-l {
        float: left;
        .header_pic {
          margin: 10px;
          height: 30px;
          width: 30px;
          display: block;
          border-radius: 50%;
          overflow: hidden;
          img {
            width: 100%;
          }
        }
      }
      .items_r {
        padding-left: 10px;
        overflow: hidden;
        padding: 10px 0;
        span {
          display: inline-block;
        }
        h3 {
          display: inline-block;
          font-size: 600;
          font-size: 15px;
        }
        .time {
          display: inline-block;
          position: absolute;
          right: 10px;
          color: #999;
          font-size: 14px;
        }
        .comment_stars {
          margin-top: 6px;
        }
        .score {
          color: #999;
          font-size: 13px;
          padding-left: 7px;
        }
        .instructions {
          margin-top: 5px;
          font-size: 14px;
          line-height: 20px;
          font-weight: 600;
          padding-right: 10px;
        }
        .chose_label {
          margin-top: 8px;
          .praise {
            font-size: 20px;
            vertical-align: middle;
          }
          .labels {
            border: 1px solid #ccc;
            background-color: transparent;
            margin: 2px 0 2px 5px;
            color: #999;
          }
        }
      }
    }
  }
}
</style>
